import React, { Component } from 'react'
import PubSubJS from 'pubsub-js'

export default class Search extends Component {

    // 定义输入框的引用，获取搜索的关键字
    keyworkElement = React.createRef()

    search = ()=> {
        const {value:keywork} = this.keyworkElement.current
        if(keywork !== '') {
            PubSubJS.publish("Search", keywork)
        }
    }

    render() {
        return (
            <div className="card">
                <div className="input-group">
                <input ref={this.keyworkElement} className="form-control" type="search" placeholder="输入关键字然后回车"></input>
                <button className="btn btn-primary" onClick={this.search}>搜索</button>
                </div>
            </div>
        )
    }
}
